package cn.sciento.wechat.enterprise.service.impl;

import com.alibaba.fastjson.JSON;
import java.util.List;
import javax.annotation.Resource;
import cn.sciento.wechat.enterprise.dto.ConvertToOpenidResultDTO;
import cn.sciento.wechat.enterprise.dto.ConvertToUseridResultDTO;
import cn.sciento.wechat.enterprise.dto.CreateDeptResultDTO;
import cn.sciento.wechat.enterprise.dto.CreateTagDTO;
import cn.sciento.wechat.enterprise.dto.CreateTagResultDTO;
import cn.sciento.wechat.enterprise.dto.DefaultResultDTO;
import cn.sciento.wechat.enterprise.dto.DeptListDTO;
import cn.sciento.wechat.enterprise.dto.DeptUserDTO;
import cn.sciento.wechat.enterprise.dto.GetJoinQrcodeResultDTO;
import cn.sciento.wechat.enterprise.dto.GetTagUserListResultDTO;
import cn.sciento.wechat.enterprise.dto.GetTagUserResultDTO;
import cn.sciento.wechat.enterprise.dto.GetUserDTO;
import cn.sciento.wechat.enterprise.dto.GetUserInfoByCodeResultDTO;
import cn.sciento.wechat.enterprise.dto.GetUserInfoByDeptIdResultDTO;
import cn.sciento.wechat.enterprise.dto.InviteUserDTO;
import cn.sciento.wechat.enterprise.dto.InviteUserResultDTO;
import cn.sciento.wechat.enterprise.dto.TagUserDTO;
import cn.sciento.wechat.enterprise.dto.TagUserResultDTO;
import cn.sciento.wechat.enterprise.dto.UpdateTagNameDTO;
import cn.sciento.wechat.enterprise.dto.WechatSyncDeptDTO;
import cn.sciento.wechat.enterprise.dto.WechatSyncUserDTO;
import cn.sciento.wechat.enterprise.service.WechatCorpAddressService;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

public class WechatCorpAddressServiceImpl implements WechatCorpAddressService {
  @Resource(name = "wdRestTemplate")
  protected RestTemplate restTemplate;
  
  public DefaultResultDTO createUser(WechatSyncUserDTO userDTO, String accessToken) {
    return (DefaultResultDTO)this.restTemplate.postForObject("https://qyapi.weixin.qq.com/cgi-bin/user/create?access_token=" + accessToken, new HttpEntity(JSON.toJSONString(userDTO), (MultiValueMap)buildHttpHeaders()), DefaultResultDTO.class);
  }
  
  public GetUserDTO getUserById(String userid, String accessToken) {
    return (GetUserDTO)this.restTemplate.getForObject("https://qyapi.weixin.qq.com/cgi-bin/user/get?access_token=" + accessToken + "&userid=" + userid, GetUserDTO.class);
  }
  
  public DefaultResultDTO updateUser(WechatSyncUserDTO userDTO, String accessToken) {
    return (DefaultResultDTO)this.restTemplate.postForObject("https://qyapi.weixin.qq.com/cgi-bin/user/update?access_token=" + accessToken, new HttpEntity(JSON.toJSONString(userDTO), (MultiValueMap)buildHttpHeaders()), DefaultResultDTO.class);
  }
  
  public DefaultResultDTO deleteUserById(String userid, String accessToken) {
    return (DefaultResultDTO)this.restTemplate.getForObject("https://qyapi.weixin.qq.com/cgi-bin/user/delete?access_token=" + accessToken + "&userid=" + userid, DefaultResultDTO.class);
  }
  
  public DefaultResultDTO batchDeleteUser(List<String> userids, String accessToken) {
    return (DefaultResultDTO)this.restTemplate.postForObject("https://qyapi.weixin.qq.com/cgi-bin/user/batchdelete?access_token=" + accessToken, new HttpEntity("{ \"useridlist\": " + JSON.toJSONString(userids) + " }", (MultiValueMap)buildHttpHeaders()), DefaultResultDTO.class);
  }
  
  public DeptUserDTO getUsersByDeptId(Long deptid, String accessToken, int fetch_child) {
    return (DeptUserDTO)this.restTemplate.getForObject("https://qyapi.weixin.qq.com/cgi-bin/user/simplelist?access_token=" + accessToken + "&department_id=" + deptid + "&fetch_child=" + fetch_child, DeptUserDTO.class);
  }
  
  public GetUserInfoByDeptIdResultDTO getUserInfoByDeptId(String accessToken, Long department_id, int fetch_child) {
    return (GetUserInfoByDeptIdResultDTO)this.restTemplate.getForObject("https://qyapi.weixin.qq.com/cgi-bin/user/list?access_token=" + accessToken + "&department_id=" + department_id + "&fetch_child=" + fetch_child, GetUserInfoByDeptIdResultDTO.class);
  }
  
  public ConvertToOpenidResultDTO convertToOpenid(String accessToken, String userid) {
    return (ConvertToOpenidResultDTO)this.restTemplate.postForObject(" https://qyapi.weixin.qq.com/cgi-bin/user/convert_to_openid?access_token=" + accessToken, new HttpEntity("{\"userid\"：" + JSON.toJSONString(userid) + "}", (MultiValueMap)buildHttpHeaders()), ConvertToOpenidResultDTO.class);
  }
  
  public ConvertToUseridResultDTO convertToUserid(String accessToken, String openid) {
    return (ConvertToUseridResultDTO)this.restTemplate.postForObject("https://qyapi.weixin.qq.com/cgi-bin/user/convert_to_userid?access_token=" + accessToken, new HttpEntity("{\"openid\"：" + JSON.toJSONString(openid) + "}", (MultiValueMap)buildHttpHeaders()), ConvertToUseridResultDTO.class);
  }
  
  public DefaultResultDTO secondAuth(String accessToken, String userid) {
    return (DefaultResultDTO)this.restTemplate.getForObject("https://qyapi.weixin.qq.com/cgi-bin/user/authsucc?access_token=" + accessToken + "&userid=" + userid, ConvertToUseridResultDTO.class);
  }
  
  public InviteUserResultDTO inviteUser(String accessToken, InviteUserDTO inviteUserDTO) {
    return (InviteUserResultDTO)this.restTemplate.postForObject("https://qyapi.weixin.qq.com/cgi-bin/batch/invite?access_token=" + accessToken, new HttpEntity(JSON.toJSONString(inviteUserDTO), (MultiValueMap)buildHttpHeaders()), InviteUserResultDTO.class);
  }
  
  public GetJoinQrcodeResultDTO getJoinQrcode(String accessToken, String size_type) {
    return (GetJoinQrcodeResultDTO)this.restTemplate.getForObject(" https://qyapi.weixin.qq.com/cgi-bin/corp/get_join_qrcode?access_token=" + accessToken + "&size_type=" + size_type, GetJoinQrcodeResultDTO.class);
  }
  
  public GetUserInfoByCodeResultDTO getUserInfoByCode(String accessToken, String code) {
    return (GetUserInfoByCodeResultDTO)this.restTemplate.getForObject("https://qyapi.weixin.qq.com/cgi-bin/user/getuserinfo?access_token=" + accessToken + "&code=" + code, GetUserInfoByCodeResultDTO.class);
  }
  
  public CreateDeptResultDTO createDept(WechatSyncDeptDTO deptDTO, String accessToken) {
    return (CreateDeptResultDTO)this.restTemplate.postForObject("https://qyapi.weixin.qq.com/cgi-bin/department/create?access_token=" + accessToken, new HttpEntity(deptDTO, (MultiValueMap)buildHttpHeaders()), CreateDeptResultDTO.class);
  }
  
  public DefaultResultDTO updateDept(WechatSyncDeptDTO deptDTO, String accessToken) {
    return (DefaultResultDTO)this.restTemplate.postForObject("https://qyapi.weixin.qq.com/cgi-bin/department/update?access_token=" + accessToken, new HttpEntity(deptDTO, (MultiValueMap)buildHttpHeaders()), DefaultResultDTO.class);
  }
  
  public DefaultResultDTO deleteDeptById(Long deptId, String accessToken) {
    return (DefaultResultDTO)this.restTemplate.getForObject("https://qyapi.weixin.qq.com/cgi-bin/department/delete?access_token=" + accessToken + "&id=" + deptId, DefaultResultDTO.class);
  }
  
  public DeptListDTO getDeptList(Long deptId, String accessToken) {
    return (DeptListDTO)this.restTemplate.getForObject("https://qyapi.weixin.qq.com/cgi-bin/department/list?access_token=" + accessToken + "&id=" + deptId, DeptListDTO.class);
  }
  
  public CreateTagResultDTO createTag(String accessToken, CreateTagDTO createTagDTO) {
    return (CreateTagResultDTO)this.restTemplate.postForObject("https://qyapi.weixin.qq.com/cgi-bin/tag/create?access_token=" + accessToken, new HttpEntity(JSON.toJSONString(createTagDTO), (MultiValueMap)buildHttpHeaders()), CreateTagResultDTO.class);
  }
  
  public DefaultResultDTO updateTagName(String accessToken, UpdateTagNameDTO updateTagNameDTO) {
    return (DefaultResultDTO)this.restTemplate.postForObject("https://qyapi.weixin.qq.com/cgi-bin/tag/update?access_token=" + accessToken, new HttpEntity(JSON.toJSONString(updateTagNameDTO), (MultiValueMap)buildHttpHeaders()), DefaultResultDTO.class);
  }
  
  public DefaultResultDTO deleteTag(String accessToken, String tagid) {
    return (DefaultResultDTO)this.restTemplate.getForObject("https://qyapi.weixin.qq.com/cgi-bin/tag/delete?access_token=" + accessToken + "&tagid=" + tagid, DefaultResultDTO.class);
  }
  
  public GetTagUserResultDTO getTagUser(String accessToken, String tagid) {
    return (GetTagUserResultDTO)this.restTemplate.getForObject("https://qyapi.weixin.qq.com/cgi-bin/tag/get?access_token=" + accessToken + "&tagid=" + tagid, GetTagUserResultDTO.class);
  }
  
  public TagUserResultDTO addTagUser(String accessToken, TagUserDTO tagUserDTO) {
    return (TagUserResultDTO)this.restTemplate.postForObject("https://qyapi.weixin.qq.com/cgi-bin/tag/addtagusers?access_token=" + accessToken, new HttpEntity(JSON.toJSONString(tagUserDTO), (MultiValueMap)buildHttpHeaders()), TagUserResultDTO.class);
  }
  
  public TagUserResultDTO deleteTagUser(String accessToken, TagUserDTO tagUserDTO) {
    return (TagUserResultDTO)this.restTemplate.postForObject("：https://qyapi.weixin.qq.com/cgi-bin/tag/deltagusers?access_token=" + accessToken, new HttpEntity(JSON.toJSONString(tagUserDTO), (MultiValueMap)buildHttpHeaders()), TagUserResultDTO.class);
  }
  
  public GetTagUserListResultDTO getTagUserList(String accessToken) {
    return (GetTagUserListResultDTO)this.restTemplate.getForObject("https://qyapi.weixin.qq.com/cgi-bin/tag/list?access_token=" + accessToken, GetTagUserListResultDTO.class);
  }
  
  protected HttpHeaders buildHttpHeaders() {
    HttpHeaders httpHeaders = new HttpHeaders();
    httpHeaders.set("Content-Type", "application/json");
    return httpHeaders;
  }
}
